#!/bin/bash

echo '创建环境变量';
export OS_USERNAME=admin;
export OS_PASSWORD=123456;
export OS_PROJECT_NAME=admin;
export OS_USER_DOMAIN_NAME=Default;
export OS_PROJECT_DOMAIN_NAME=Default;
export OS_AUTH_URL=http://controller:35357/v3;
export OS_IDENTITY_API_VERSION=3;

echo '创建 domain, projects, users, roles';
echo '1.创建域';
openstack domain create --description "Domain" example;
echo '2.创建服务项目';
openstack project create --domain default   --description "Service Project" service;
echo '3.创建平台demo项目';
openstack project create --domain default --description "Demo Project" demo;
echo '4.创建demo用户，设置密码：123456';
openstack user create --domain default  --password 123456 demo;
echo '5.创建用户角色';
openstack role create user;
echo '6.添加用户角色user到demo项目和demo用户';
openstack role add --project demo --user demo user;

# 验证操作 : ---------------似乎可以不验证-----------------
# 1.取消环境变量';
#unset OS_AUTH_URL OS_PASSWORD;
# 2.admin用户返回的认证token：密码123456';
#echo '密码123456';
#openstack --os-auth-url http://controller:35357/v3 --os-project-domain-name Default --os-user-domain-name Default --os-project-name admin --os-username admin token issue;

# '3.demo用户返回的认证token：密码：123456';
#echo '密码123456';
#openstack --os-auth-url http://controller:5000/v3 --os-project-domain-name Default --os-user-domain-name Default --os-project-name demo --os-username demo token issue;
# '完成验证';
#echo '         完成验证，开始创建脚本         ';


echo '创建admin-openrc脚本';
echo 'export OS_PROJECT_DOMAIN_NAME=Default
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_NAME=admin
export OS_USERNAME=admin
export OS_PASSWORD=123456
export OS_AUTH_URL=http://controller:5000/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2'>/root/admin-openrc;

echo '创建demo-openrc脚本';
echo 'export OS_PROJECT_DOMAIN_NAME=Default
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_NAME=demo
export OS_USERNAME=demo
export OS_PASSWORD=123456
export OS_AUTH_URL=http://controller:5000/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2'>/root/demo-openrc;

echo '验证脚本admin-openrc';
source /root/admin-openrc;
openstack token issue;

echo '-----------------------------------------------------'
echo '          domain, projects, users, roles 配置完成！  '
echo '-----------------------------------------------------'
